package cc.wsyw126.java.nowcoder.sword_offer;


import java.util.Stack;

/**
 * Created by Administrator on 2017/4/8.
 */
public class StackMin {
    private Stack<Integer> data = new Stack<>();
    private Stack<Integer> minValue = new Stack<>();


    public void push(int node) {
        if (data.size() == 0) {
            data.push(node);
            minValue.push(node);
        } else {
            Integer peek = data.peek();
            data.push(node);
            if (node < peek) {
                minValue.push(node);
            } else {
                minValue.push(peek);
            }
        }

    }

    public void pop() {
        if (data.size() > 0) {
            data.pop();
            minValue.pop();
        }
    }

    public int top() {
        Integer peek = data.peek();
        return peek;
    }

    public int min() {
        return minValue.peek();
    }


    public static void main(String[] args) {
        StackMin stackMin = new StackMin();
        stackMin.push(3);
        stackMin.push(4);
        stackMin.push(2);
        stackMin.push(1);
        stackMin.pop();
        stackMin.pop();
        stackMin.push(0);

        System.out.println(stackMin.min());
    }
}
